<template>
    <el-container>
        <el-main class="wrapper">
            <section class="panel cms-content">
                <div class="panel-body">
                    <div class="cms-btn-group">
                        <el-button type="primary" icon="fa fa-plus" @click="create()" size="small"> 新增</el-button>
                        <el-button type="warning" icon="fa fa-refresh" @click="list()" size="small"> 刷新</el-button>
                        <el-button type="info" icon="fa fa-trash" @click="multiDelete()" size="small"> 删除</el-button>
                    </div>
                    <div class="cms-search-group">
                        <div class="cms-search-item">
                            <el-input size="medium" clearable v-model="params.kwd" placeholder="请输入标题"
                                      class="input-with-select">
                                <el-button type="primary" @click="search()" slot="append"
                                           icon="el-icon-search"></el-button>
                            </el-input>
                        </div>
                        <div class="cms-search-item" style="width: 120px">
                            <el-select size="medium" v-model="params.position">
                                <el-option label="推荐(全部)" value=""></el-option>
                                <el-option label="正常" :value="1"></el-option>
                                <el-option label="推荐" :value="2"></el-option>
                            </el-select>
                        </div>
                        <div class="cms-search-item" style="width: 120px">
                            <el-select size="medium" v-model="params.status">
                                <el-option label="状态(全部)" value=""></el-option>
                                <el-option label="正常" :value="1"></el-option>
                                <el-option label="锁定" :value="2"></el-option>
                            </el-select>
                        </div>
                        <div class="cms-search-item">
                            <el-date-picker size="medium"
                                            v-model="date"
                                            align="right"
                                            type="daterange"
                                            unlink-panels
                                            range-separator="至"
                                            start-placeholder="开始日期"
                                            end-placeholder="结束日期"
                                            :picker-options="pickerOptions" @change="pickerChange">
                            </el-date-picker>
                        </div>
                    </div>
                    <el-table ref="multipleHandle"
                              v-loading="loading"
                              element-loading-text="努力加载中..."
                              element-loading-spinner="el-icon-loading"
                              @selection-change="handleSelectionChange"
                              tooltip-effect="dark" :data="listData.data"  border>
                        <el-table-column align="center" type="selection" width="30"></el-table-column>
                        <el-table-column align="center" prop="id" label="编号" width="60"></el-table-column>
                        <el-table-column prop="content" label="内容" width="550" :show-overflow-tooltip="true"></el-table-column>
                        <el-table-column prop="source" label="来源" width="150"></el-table-column>
                        <el-table-column align="center" prop="status_text" label="状态" width="75">
                            <template slot-scope="scope"><span v-html="scope.row.status_text"></span></template>
                        </el-table-column>
                        <el-table-column align="center" prop="position_text" label="推荐" width="80">
                            <template slot-scope="scope"><span v-html="scope.row.position_text"></span></template>
                        </el-table-column>
                        <el-table-column prop="created_at" label="创建日期" width="160"></el-table-column>
                        <el-table-column prop="created_at" label="最近修改" width="160"></el-table-column>
                        <el-table-column label="操作" >
                            <template slot-scope="scope">
                                <el-button @click="edit(scope.row)" icon="fa fa-edit" type="primary" size="mini" plain> 编辑</el-button>
                                <el-button @click="destory(scope.row)" icon="fa fa-trash" type="info" size="mini" plain> 删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <pagination :pageData="listData" :params="params" @list="list"/>
                </div>
            </section>
        </el-main>
        <!-- 表单弹窗-->
        <el-dialog
                v-if="dialog.show"
                :title="dialog.title"
                custom-class="cms-dialog"
                :top="dialog.top"
                :visible.sync="dialog.show"
                :width="dialog.width">
            <el-form :model="form" ref="form" label-width="80px">
                <el-form-item label="内容：" :rules="[{ required: true, message: '请填写快讯内容', trigger: 'blur'}]" prop="content">
                    <el-input type="textarea" rows="5" v-model="form.content" placeholder="请填写快讯内容"></el-input>
                </el-form-item>
                <el-form-item :rules="[{ required: true, message: '请输入快讯来源', trigger: 'blur'}]" label="来源：" prop="source">
                    <el-input v-model="form.source" placeholder="请输入快讯来源"></el-input>
                </el-form-item>
                <el-form-item label="状态：" prop="status">
                    <el-radio-group v-model="form.status">
                        <el-radio :label="1" border>正常</el-radio>
                        <el-radio :label="2" border>锁定</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="推荐：" prop="position">
                    <el-radio-group v-model="form.position">
                        <el-radio border :label="1">正常</el-radio>
                        <el-radio border :label="2">推荐</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialog.show = false">取 消</el-button>
                <el-button type="primary" @click="update()">确 定</el-button>
              </span>
        </el-dialog>
    </el-container>
</template>

<script>
    import {mapGetters} from 'vuex';
    import {LiveList,LiveUpdate,LiveDestroy} from '@/utils/request';
    import Pagination from '@/components/pagination';
    export default {
        components: {Pagination},
        data() {
            return {
                listData: [],//列表数据
                params: {page: 1, kwd:'',status:'',position:'',start: '',end: ''},
                multipleSelection:[],//多选项
                dialog: {
                    title: '',
                    top: '10vh',
                    show: false,
                    width: '700px'
                },
                form:null,
                date: '',
                //日期筛选
                pickerOptions: {
                    shortcuts: [{
                        text: '今天',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '昨天',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '一周',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '一个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                            picker.$emit('pick', [start, end]);
                        }
                    }]
                },
            }
        },
        activated(){
            this.list();
        },
        computed: {
            ...mapGetters(['active','loading']),
        },
        methods: {
            //时间筛选
            pickerChange() {
                if (this.date) {
                    this.params.start = this.moment(this.date[0]).format('YYYY-MM-DD');
                    this.params.end = this.moment(this.date[1]).format('YYYY-MM-DD');
                } else {
                    this.params.start = '';
                    this.params.end = '';
                }
            },
            //新增
            create(){
                this.form = {
                    id:0,
                    content:'',
                    source:'',
                    status:1,
                    position:1,
                };
                this.dialog.title = '新增快讯';
                this.dialog.show = true;
            },
            //编辑
            edit(info){
                this.form = {
                    id: info.id,
                    content: info.content,
                    source: info.source,
                    status: info.status,
                    position: info.position,
                };
                this.dialog.title = '编辑快讯';
                this.dialog.show = true;
            },
            //列表
            list(){
                this.$store.dispatch('setLoading', true);
                LiveList({params: this.params}).then((res)=>{
                    if (res.code) {
                        this.$message.error(res.msg);
                    } else {
                        this.listData = res.data;
                    }
                    this.$store.dispatch('setLoading', false);
                })
            },
            //更新
            update(){
                this.$refs['form'].validate((valid) => {
                    if (valid) {
                        LiveUpdate(this.form).then((res)=>{
                            if(res.code){
                                this.$message.error(res.msg);
                            }else{
                                this.$message({
                                    type: 'success',
                                    duration: '1000',
                                    message: res.msg,
                                    onClose:() =>{
                                        this.dialog.show = false;
                                        this.list();
                                    }
                                });
                            }
                        });
                    }
                });
            },
            handleSelectionChange(val) {
                let select = [];
                if(val.length > 0){
                    val.filter((item)=>{
                        if(select.indexOf(item.id) === -1){
                            select.push(item.id)
                        }
                    });
                    this.multipleSelection = select;
                }else{
                    this.multipleSelection = [];
                }
            },
            //筛选
            search() {
                this.params.page = 1;
                this.list();
            },
            //删除
            destroy(id) {
                this.$confirm('确定要删除选择的快讯信息吗？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    customClass: 'cms-message-box',
                    type: 'warning'
                }).then(() => {
                    LiveDestroy({id: id}).then((res) => {
                        if (res.code) {
                            this.$message.error(res.msg);
                        } else {
                            this.list();
                        }
                    });
                }).catch(() => {});
            },
            // 多选删除
            multiDelete(){
                if(this.multipleSelection.length == 0){
                    this.$message.error('请至少选择一项你要删除的项');return false;
                }
                this.destroy(this.multipleSelection);
            },
        }
    }
</script>

<style scoped>

</style>
